package cn.hiapi.shopkeeper.service.user;

import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.redis.service.RedisService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.config.IConfigService;
import cn.hiapi.interfaces.user.IUserCreateSuccess;
import cn.hiapi.interfaces.user.data.UserData;
import cn.hiapi.shopkeeper.consts.AppConst;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.activity.ActivityLottery;
import cn.hiapi.shopkeeper.entity.activity.ActivityOther;
import cn.hiapi.shopkeeper.entity.order.ActivityInviteRecord;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.service.activity.ActivityFactory;
import cn.hiapi.shopkeeper.service.activity.ActivityOtherService;
import cn.hiapi.shopkeeper.service.activity.ActivityService;
import cn.hiapi.shopkeeper.service.order.ActivityInviteRecordService;
import cn.hiapi.utils.DateUtils;
import cn.hiapi.utils.JsonUtils;
import cn.hiapi.utils.TaskExecutor;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author AdinZ
 * @date 2023/1/4 0:13
 */
@Service
@Slf4j
public class ActivityInviteNewUserCallback implements IUserCreateSuccess {

    private final DispatchContext dispatchContext;
    private final String LOTTERY_HOME = "CORE:LOTTERY_AD:INVITE";

    public ActivityInviteNewUserCallback(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }

    private String getLotteryKey(long uid, long aid) {
        return new StringBuffer(DateUtils.format(DateUtils.FORMAT_YYYY_MM_DD)).append("-").append(uid).append("-").append(aid).toString();
    }

    @Override
    public void createSuccess(UserData user) {
        ActivityInviteRecord newReg = new ActivityInviteRecord();
        newReg.setUid(user.getUid());
        newReg.setInviteUid(user.getUid());
        newReg.setAid(AppConst.ACTIVITY_LOTTERY_HOME);
        newReg.setShopId(0);
        newReg.setRemark("新用户注册送首页抽奖次数");
        newReg.setType(ActivityType.LOTTERY);
        this.dispatchContext.getServiceOne(ActivityInviteRecordService.class).save(newReg);


        if (user.getOther() == null) return;
        final JSONObject other = (JSONObject) user.getOther();
        final long aid = other.getLongValue("aid");
        if (user.getInviteUid() == null) return;


        TaskExecutor.getInstance().execute(() -> {
            if (aid > 0) {
                Activity activity = this.dispatchContext.getServiceOne(ActivityService.class).get(aid);
                if (activity != null && activity.getType() == ActivityType.LOTTERY) {

                    {
//                        新用户在送一次,分享活动次数
                        ActivityInviteRecord record = new ActivityInviteRecord();
                        record.setUid(user.getUid());
                        record.setInviteUid(user.getUid());
                        record.setAid(aid);
                        record.setShopId(activity.getShopId());
                        record.setType(ActivityType.LOTTERY);
                        record.setRemark("通过分享抽奖注册的新用户");
                        this.dispatchContext.getServiceOne(ActivityInviteRecordService.class).save(record);
                    }

                    ActivityOther activityOther = this.dispatchContext.getServiceOne(ActivityOtherService.class).getByAid(activity.getId());
                    if (activityOther != null) {
                        long increment = this.dispatchContext.getServiceOne(RedisService.class).increment(LOTTERY_HOME, this.getLotteryKey(user.getInviteUid(), activity.getId()), 1);
                        if (increment > activityOther.getDayShare()) {
                            log.info("[活动拉新] 超出每天分享次数,本拉新不算:{}", user);
                            return;
                        }
                        for (int i = 0; i < activityOther.getShareLottery(); i++) {
                            ActivityInviteRecord record = new ActivityInviteRecord();
                            record.setUid(user.getUid());
                            record.setInviteUid(user.getInviteUid());
                            record.setAid(aid);
                            record.setShopId(activity.getShopId());
                            record.setType(ActivityType.LOTTERY);
                            record.setRemark("通过分享邀请新用户获得抽奖次数");
                            this.dispatchContext.getServiceOne(ActivityInviteRecordService.class).save(record);
                        }
                        return;
                    }
                }
            }

            JSONObject config = this.dispatchContext.getServiceOne(IConfigService.class).getConfig(AppConst.CONFIG_SHOPKEEPER_CONFIG);
            //每天抽奖次数
            int lottery_count = JsonUtils.getInt(config, "lottery_count", 0);
            //分享获得抽奖次数
            int share_lottery = JsonUtils.getInt(config, "share_lottery", 0);
            //每天最多分享次数
            int share_limit = JsonUtils.getInt(config, "share_limit", 1);

            long increment = this.dispatchContext.getServiceOne(RedisService.class).increment(LOTTERY_HOME, this.getLotteryKey(user.getInviteUid(), AppConst.ACTIVITY_LOTTERY_HOME), 1);
            if (increment > share_limit) {
                log.info("[活动拉新] [首页分享]  超出单日分享上线,跳过  用户手机号:{}", user.getMobile());
                return;
            }
            for (int i = 0; i < share_lottery; i++) {
                ActivityInviteRecord record = new ActivityInviteRecord();
                record.setUid(user.getUid());
                record.setInviteUid(user.getInviteUid());
                record.setAid(AppConst.ACTIVITY_LOTTERY_HOME);
                record.setShopId(0);
                record.setRemark("邀请新用户获得首页抽奖次数");
                record.setType(ActivityType.LOTTERY);
                this.dispatchContext.getServiceOne(ActivityInviteRecordService.class).save(record);
            }
        });

    }
}
